---@class UIClimbTowerSort : UIBase
UIClimbTowerSort = UIBase.New(PanelNames.UIClimbTowerSort)
local t = UIClimbTowerSort

local effect_mat = nil

function t.Init(towerType)
    t.towerType = towerType
    UIBase.Init(t)
end

function t:OnCreate()
    self.btnClose = self:GetChild("btnClose")
    self.bgClose = self:GetChild("bgClose")
    self.rankList = self:GetList("ranklist/ranklist")
    self.my = self:GetChild("my")
    self.typeController = self:GetController("type");
    self.nothingController = self:GetController("nothing");
    self.advancedController = self:GetController("open");
    self:RegisterOnClick(self.btnClose, self.ClickClose)
    self:RegisterOnClick(self.bgClose, self.ClickClose)
    self:RegisterOnClick(self:GetChild("btnTower1"),function() self:OnClickChangeType(2) end);
    self:RegisterOnClick(self:GetChild("btnTower2"),function() self:OnClickChangeType(3) end);
    self:RegisterOnClick(self:GetChild("btnTower3"),function() self:OnClickChangeType(4) end);
    self:RegisterOnClick(self:GetChild("btnMain"),function() self:OnClickChangeType(1) end);
    self:SetDynamicList(self.rankList, self.ItemRender)

    self.poolMgr = ComponentPoolMgr.New(t.RankItem)
    self.myItem = self.poolMgr:GetBindScript(self.my)
    self.datas = nil

    self:RegisterEvent(Config.EventType.Fresh_TowerRanks, self.GetAllData)
    self:RegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);

    if logicMgr.FuncOpenManager.isOpen(Config.FuncOpenType.TowerAdvaced, false) then
        self.advancedController.selectedIndex = 1
        self.typeController.selectedIndex = self.towerType - 1
    else
        self.advancedController.selectedIndex = 0
    end
end

function t:OnEnable()
    self.rankList.numItems = 0
    --self:Refresh()
    logicMgr.TowerManager.sendGetRanks(self.towerType)
end

function t:OnClickChangeType(type)
    self.towerType = type
    self.rankList.numItems = 0
    --self:Refresh()
    logicMgr.TowerManager.sendGetRanks(type)
end

function t:OnDisable()
end

function t:OnDestroy()
    if self.poolMgr then
        self.poolMgr:OnDestroy()
    end
    self:UnRegisterEvent(Config.EventType.FreashRoleInfoOther, self.GotOtherShowInfo);

    self.datas = nil
    self.poolMgr = nil
    self.myItem = nil
end

function t:Refresh()
    t.datas = logicMgr.TowerManager.getRanks(self.towerType)
    local rankNum = #self.datas
    self.rankList.numItems = Config.Const_Rank_Max_Num--rankNum --TODO RANK
    HelperFunc.ListTranstionPlay(self.rankList)

    if rankNum > 0 then
        self.nothingController.selectedIndex = 0;
        local myRank = logicMgr.TowerManager.getMyRank(self.towerType)
        self.myItem:Refresh(myRank.rank, myRank.data)
    else
        local myRank = logicMgr.TowerManager.getMyRank(self.towerType)
        self.myItem:Refresh(myRank.rank, myRank.data)
        self.nothingController.selectedIndex = 1;
    end
end

function t:ItemRender(index, go)
    local data = self.datas[index]
    local item = self.poolMgr:GetBindScript(go)
    item:Refresh(index, data)

    if effect_mat == nil then
        local head = go:GetChild("head")
        local effect = head:GetChild("effect_FirstRank")
        if effect then
            local t = effect.displayObject.cachedTransform:Find("GoWrapper/effect_FirstRank_SkeletonData")
            local anim = t:GetComponent("SkeletonAnimation")
            effect_mat = Util.GetMaterialOnSkeletonAnimation(anim, "effect_FirstRank_Material")
            if effect_mat then
                effect_mat:SetFloat("_StencilComp", 3)
            end
        end
    end
end

function t:ClickClose()
    closeUI(PanelNames.UIClimbTowerSort)
end

function t:GetAllData()
    t:Refresh()
end

function t:GotOtherShowInfo(info)
    showUI(PanelNames.UIPlayerShow, info,nil,false,function()
        UIBottom.enterAdventure()
        showUI(PanelNames.UIClimbTower)
    end)
end

t.RankItem = Class("rankItem", UIComponentInterface)
---@class UIClimbTowerSort_Item : UIComponentInterface
local t_t = t.RankItem
function t_t:OnCreate()
    self.rankText = self:GetChild("rank")
    self.pointText = self:GetChild("point")
    self.nameText = self:GetChild("name")
    self.lvText = self:GetChild("level")
    self.icon = self:GetChild("head/head/HeadIcon")
    self.frame = self:GetChild("head/frame")
    self.head = self:GetChild("head")
    self.ctrlNoRank = self:GetController("ctrlNoRank")
    self.ctrlRank = self:GetController("rank")
    self.ctrlShow = self:GetController("c1")
    self.head=self:GetChild("head")
end
function t_t:OnDestroy()
    self.data = nil

    if effect_mat then
        effect_mat:SetFloat("_StencilComp", 8)
    end
end

--- @param rankIdx number
--- @param data TowerRankInfo
function t_t:Refresh(rankIdx, data)
    self.data = data

    if data == nil then
        self.ctrlShow.selectedIndex = 1
        return
    end
    if data.playerId == logicMgr.HeroManager.GetRoleId() then
        self.ctrlShow.selectedIndex = 2
    elseif rankIdx > #UIClimbTowerSort.datas then
        self.ctrlShow.selectedIndex = 1
        return
    else
        self.ctrlShow.selectedIndex = 0
    end

    self.rankText.text = rankIdx
    self.pointText.text = tostring(data.score)
    self.nameText.text = data.name
    self.lvText.text = data.level
    self.head.onClick:Clear();
    self.head.onClick:Add(function()
        if tostring(data.playerId) ~= logicMgr.HeroManager.GetRoleId() then
            roleInfoHandler.sendreqOtherRoleInfo(data.playerId);
        end
    end)
    if(rankIdx==1)then
        self.head:GetController("first").selectedIndex=1
    else
        self.head:GetController("first").selectedIndex=0
    end
    self:SetPlayerIcon(self.icon, data.headIcon, self.frame, data.headFrame)
    if rankIdx <= 0 then
        self.ctrlNoRank.selectedIndex = 1
    else
        self.ctrlNoRank.selectedIndex = 0
        self.ctrlRank.selectedIndex = rankIdx >= 4 and 3 or (rankIdx - 1)
    end

   
end

return t